﻿using module ..\02.Runner.ps\SimpleRunner.psd1
using module ..\02.Runner.ps\CalcRunner.psd1
using module ..\02.Runner.ps\TaxCalcRunner.psd1
using module ..\02.Runner.ps\SimpleRunner.psm1
using module ..\02.Runner.ps\CalcRunner.psm1
using module ..\02.Runner.ps\TaxCalcRunner.psm1

function Main()
{
    $Error.Clear();
    Add-Type -Assembly System.Windows.Forms

    #各種設定
    $currentPath = Get-Location
    $target           = '{0}\..\01.TestTarget\CalcApp.exe' -F $currentPath
    $testSetRoot      = '{0}\..\04.TestSet\'  -F $currentPath
    $testExpectedRoot = '{0}\..\05.TestExpected\' -F $currentPath
    $resultRoot       = '{0}\..\06.TestResult\{1}\' -f $currentPath,( [System.DateTime]::Now.ToString('yyyyMMdd_HHmmss') )
    [void](New-Item $resultRoot -ItemType Directory)

    #テストセットの一覧を読み込む
    $testSetList = @(Get-Content '.\TestList.txt' -Encoding UTF8) 

    $errorFlag = $false

    #テストセットの実行処理
    $testSetCount = 0
    ForEach( $testSet in $testSetList )
    {
        # #から始まる行はコメントとして処理しない
        if( ( $testSet -ne '' ) -and ( $testSet.SubString(0,1) -ne '#' ))
        {
            $testSetPath  = $testSetRoot + $testSet
            $testLogPath  = $resultRoot + $testSet + '.log'
            [void]( RunTestSet $testSetPath $testLogPath )

            $result = CheckLog $testExpectedRoot $testLogPath
            if( $result -ne $null )
            {
                $errorFlag = $true
                [System.Windows.Forms.MessageBox]::Show( ( "テストエラー[{0}]`n{1}" -f $testSet,$result), "エラー" )
            }

            $testSetCount++
        }
    }

    if( $errorFlag -eq $false )
    {
        [System.Windows.Forms.MessageBox]::Show( ('テスト成功 実行テストセット数[{0}]' -f $testSetCount ) , "成功" )
    }

}

function RunTestSet([string] $testSetPath, [string]$testLogPath )
{
    $testCaseList = @(Get-ChildItem $testSetPath)

    ForEach( $testCase in $testCaseList )
    {
        if($testCase.Extension.ToUpper() -ne ".TXT")
        {
            continue;
        }
        $testKeyValue = @(Get-Content $testCase.FullName -Encoding UTF8) 
        $index = $testKeyValue[0].IndexOf('#Runner=')
        if( $index -eq -1 )
        {
            continue;
        }
        $runnerName = $testKeyValue[0].SubString(($index+8) );
        $testRunner = $null
        if( $runnerName -eq 'CalcRunner' )
        {
            $testRunner  = [CalcRunner]::new();
        }
        elseif( $runnerName -eq 'SimpleRunner' )
        {
            $testRunner  = [SimpleRunner]::new();
        }
        elseif( $runnerName -eq 'TaxCalcRunner' )
        {
            $testRunner  = [TaxCalcRunner]::new();
        }

         
        if( $testRunner -eq $null )
        {
            continue;
        }
        $testRunner.TargetPath  = $target;
        $testRunner.LogFilePath = $testLogPath;
        $testRunner.WriteLog( ( "※※※テストケース[{0}]※※※" -F $testCase.Name ) );
        $testRunner.SetUp();
        $testRunner.テスト実行( $testKeyValue ); 
        $testRunner.TearDown();
    }

}

function CheckLog([string] $testExpectedRoot, [string]$testLogPath, [string]$msg )
{
    $result = $null
    $logFileInfo = [System.IO.FileInfo]::new( $testLogPath )

    $testExpectedFilePath = '{0}{1}' -f $testExpectedRoot,$logFileInfo.Name

    $logLines = @(Get-Content $testLogPath -Encoding UTF8) 
    $expectedLines = @(Get-Content $testExpectedFilePath -Encoding UTF8) 

    if( $logLines.Length -eq $expectedLines.Length )
    {
        $result = $null
        For( $i=0;$i -lt $logLines.Length; $i++ )
        {
            if( $logLines[$i] -ne $expectedLines[$i] )
            {
                $result = "NG`t[{0}]`nEx`t[{1}]" -f $logLines[$i],$expectedLines[$i]
                break
            }
        }
    }
    else
    {
        $result = "NG 行数違い";
    }

    return $result
}

main
